// popup openaccount
$(document).ready(function(){
	var width_popup = ($(window).width()-$('.popup-openaccount').width())/2;
	var height_popup = ($(window).height()-$('.popup-openaccount').height())/2;
	
	var width_popup_forgotpassword = ($(window).width()-$('.popup-forgotpassword').width())/2;
	var height_popup_forgotpassword = ($(window).height()-$('.popup-forgotpassword').height())/2;
	
	if(height_popup >0)
	{
		$('.popup-openaccount').css('top',height_popup+'px');
		$('.popup-forgotpassword').css('top',height_popup_forgotpassword+'px');
	} else {
		$('.popup-openaccount').css('top','0px');
		$('.popup-forgotpassword').css('top','0px');
	}
	if(width_popup >0)
	{
		$('.popup-openaccount').css('left',width_popup+'px');
		$('.popup-forgotpassword').css('left',width_popup_forgotpassword+'px');
	} else {
		$('.popup-openaccount').css('left','0px');
		$('.popup-forgotpassword').css('left','0px');
	}
	$('.openaccount, .forgot').click(function(){
		var width=$(document).width();
		var height=$(document).height();
		$('.dialog3').css('display','block');
		$('.dialog3').css('opacity','0');
		$('.dialog3').css('width',width+'px');
		$('.dialog3').css('height',height+'px');
		
		var selected = $(this).attr("class");
		
		if(document.all)
		{
			$('select').css('display','none');
		}
		$('.dialog3').animate({
				opacity:0.5
		}, 300, function() {
				if (selected == "openaccount") {
					$('.popup-openaccount').css('display','block');
				} else if (selected == "forgot") {
					$('.popup-forgotpassword').css('display','block');
				}
			$('.dialog3').click(function(){
				if (selected == "openaccount") {
					$('.popup-openaccount').css('display','none');							
				} else if (selected == "forgot") {
					$('.popup-forgotpassword').css('display','none');
				}					
				
				$('.dialog3').animate({
					opacity:0
				  }, 300, function() {
					$('.dialog3').css('display','none');
					$('.dialog3').unbind('click');
					if(document.all)
					{
						$('select').css('display','block');
					}	
				  });				
			});
		});
		return false;
	});
		
	//SLIDE OPEN ACCOUNT		
	$(".prevstep").click(function(){
		$('.openaccount-wrap-slide_inner').animate({left: 0});
		$('.steps li.last').removeClass('current');
		$('.steps li.first').addClass('current');		
	});
	
	$('.step-1 .nextstep').click(function() {
		checkValidStep1();
		if ($(".step-1 input[type=text].error").length == 0) {
			var action = $("#register_form").attr('action');
			var form_data = getFormValue('step-1');
			$.ajax({
				type: "POST",
				url: action,
				data: form_data,
				dataType: "json",
				success: function(response)
				{
					//Effect next step
					$('.openaccount-wrap-slide_inner').animate({left: -763});
					$('.steps li.first').removeClass('current');
					$('.steps li.last').addClass('current');
					
					$("#reg_message").html("");
					if (response.success == true) {
						//Set name and email step 2
						$(".step-2 .right_inner .register-step2 div:eq(0) span").html(form_data.username);
						$(".step-2 .right_inner .register-step2 div:eq(1) span").html(form_data.email);
						
						//Set message
						$(".register-step2").show();
					} else {
						$(".register-step2").hide();
						$("#reg_message").html(response.message);
					}
				}
			});
		}
		return false;
	});
	
	$('form').submit(function() {
		checkValidStep2();
		if ($(".step-2 .error").length == 0) {	
			var action = $("#register_form").attr('action');
			var form_data = getFormValue('step-2');
			$.ajax({
				type: "POST",
				url: action,
				data: form_data,
				dataType: "json",
				success: function(response)
				{
					if (response.success == true) {
						$(".register-step2").hide();
						$("#reg_message").html(response.message);
					} else {
						$(".register-step2").hide();
						$("#reg_message").html(response.message);
					}
				}
			});
			return false;
		}
		
		return false;
	});
	
	function getFormValue(step) {
		var form_data = {
				username: $("#openaccount-username").val(),
				email: $("#openaccount-email").val(),
				day: $(".day").val(),
				month: $(".month").val(),
				year: $(".year").val(),
				pwd: $("#pwd").val(),
				is_ajax: 1,
				step: step
			};
		return form_data;
	}
	
	//CLOSE POPUP
	$('.button-close-popup').click(function() {
		$('.popup-openaccount, .popup-forgotpassword, .dialog3').css('display','none');
	});
	
	
	function checkValidStep1 () {
		var username = $("#openaccount-username").val();
		var email = $("#openaccount-email").val();
		
		if (username.length == 0) {
			$("#openaccount-username").addClass("error");
		} else {
			$("#openaccount-username").removeClass("error");
		}
		
		var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
		if(!pattern.test(email)){
			$("#openaccount-email").addClass("error");
		} else {
			$("#openaccount-email").removeClass("error");
		}
		
	}
	
	function checkValidStep2() {
		var day = $(".day").val();
		var month = $(".month").val();
		var year = $(".year").val();
		
		var password = $("input[type=password]:eq(0)").val();
		var repassword = $("input[type=password]:eq(1)").val();
		
		
		if (day == "Day") {
			$(".day").addClass("error");
		} else {
			$(".day").removeClass("error");
		}
		
		if (month == "Month") {
			$(".month").addClass("error");
		} else {
			$(".month").removeClass("error");
		}
		
		if (year == "Year") {
			$(".year").addClass("error");
		} else {
			$(".year").removeClass("error");
		}
		
		if (password.length < 2 || password.length > 8) {
			$("input[type=password]:eq(0)").addClass("error");
		} else {
			$("input[type=password]:eq(0)").removeClass("error");
		}
		
		if (repassword != password) {
			$("input[type=password]:eq(1)").addClass("error");
		} else {
			$("input[type=password]:eq(1)").removeClass("error");
		}
		
		if (!$('.term-accept input[type=checkbox]').is(':checked')) {
			$(".term-accept input[type=checkbox]").addClass("error");
		} else {
			$(".term-accept input[type=checkbox]").removeClass("error");
		}
	}
});